package demo;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

//网络编程题
public class Day22001 {
    public static void main(String[] args) throws Exception {
        Socket s = new Socket("127.0.0.1",8888);
        //键盘输入数据,并发给服务器
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一句话:");
        String next = sc.next();
        s.getOutputStream().write(next.getBytes());
        s.shutdownOutput();//让服务器端结束循环读的行为
        //接受服务器的响应
        InputStream in = s.getInputStream();
        byte[] arr = new byte[1024*8];
        int i;
        while ((i=in.read(arr))!=-1){
            System.out.print(new String(arr,0,i));
        }
        System.out.println();
        s.close();
    }
}
class MyServer{
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(8888);
        Socket s = ss.accept();//接受客户端请求
        //读数据
        InputStream in = s.getInputStream();
        byte[] arr = new byte[1024*8];
        int i;
        //创建sb,用于保存每次循环收到的客户端发送的数据
        StringBuilder stringBuilder = new StringBuilder();
        while ((i=in.read(arr))!=-1){
            stringBuilder.append(new String(arr,0,i));
        }
        System.out.println(stringBuilder);
        s.getOutputStream().write(stringBuilder.reverse().toString().getBytes());
        s.shutdownOutput();
        s.close();
        ss.close();
    }
}
